import pygame
import sys

pygame.init()
pygame.display.init()

width = 384
height = 288
zoom = 2

_screen = pygame.display.set_mode((width * zoom, height * zoom))
screen = pygame.surface.Surface((width, height))

ken_frames = []

for i in range(14657, 15706):
    fname = "images/ken/" + str(i) + ".png"
    try:
        img = pygame.image.load(fname)
        img.convert()
        ken_frames.append(img)
    except(pygame.error):
        pass
        print("Failed to open " + fname)

framerate = 15

frame = 0

clock = pygame.time.Clock()
hold = True

font = pygame.font.SysFont("Courier New", 16, True, False)

while 1:
	clock.tick(framerate)
	for evt in pygame.event.get():
		if evt.type == pygame.QUIT:
			sys.exit()
			
		if evt.type == pygame.KEYDOWN:
			if evt.key == pygame.K_1:
				framerate = 15
				hold = False
			elif evt.key == pygame.K_2:
				framerate = 15
				hold = True
			elif evt.key == pygame.K_3:
				framerate = 30
				hold = True
			elif evt.key == pygame.K_4:
				framerate = 60
				hold = True
			elif evt.key == pygame.K_RIGHT:
				frame = (frame + 1) % len(ken_frames)
			elif evt.key == pygame.K_LEFT:
				frame = (frame - 1) % len(ken_frames)
				
	screen.fill((255,255,255))
				
	keys = pygame.key.get_pressed()
	
	if hold:
		if keys[pygame.K_LEFT]:
			frame = (frame - 1) % len(ken_frames)
		if keys[pygame.K_RIGHT]:
			frame = (frame + 1) % len(ken_frames)
		
	screen.blit(ken_frames[frame], (0,0))
	text = font.render(str(frame), False, (255,0,0))
	screen.blit(text, (0,0))

	pygame.transform.scale(screen, (width * zoom, height * zoom), _screen)
	pygame.display.update()